<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class Post extends MY_Controller
{

    public function index()
    {
        
    }

    /**
     * 查看房源信息详细
     * @param int $postId 信息ID
     */
    public function view( $postId )
    {
        $postId = intval( $postId );
        $postData = $this->model_post_getById( $postId );
        if ( empty( $postData ) )
        {
            $messageData = array(
                'title' => '没有该房源信息',
                'message' => '抱歉，我没法帮你找到该房源信息，4秒后跳转回首页',
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => config_item( 'base_url' ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data = array(
                'title' => '抱歉，该信息没找到',
                'messageBox' => $messageBox,
            );
        }
        else
        {
            //获取房源图片
            $postImg = $this->model_attachment_getByPostId( $postData['id'] );
            //该地段其他房源
            $relativeInfo = $this->model_post_rentGetByZone( $postData['zone'], 5 );
            //留言/评论框
            $commentBox = Modules::run( 'commentBox', $postId );
            $data = array(
                'title' => "{$postData['title']}信息详细",
                'postId' => $postId,
                'postData' => $postData,
                'postImg' => $postImg,
                'relativeInfo' => $relativeInfo,
                'commentBox' => $commentBox,
            );
        }

        $this->get_common_section( $data );
        $this->load->view( 'post/view', $data );
    }

    /**
     * 接收评论并处理
     */
    public function comment()
    {
        if ( !$this->session->validUser() || !chk_permission( 'comment' ) )
        {
            $messageData = array(
                'title' => '操作失败',
                'message' => '抱歉，您没有此操作的权限，4秒后跳转回首页',
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => config_item( 'base_url' ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data = array(
                'title' => '操作失败',
                'messageBox' => $messageBox,
            );
        }
        else
        {
            if ( $this->input->post( 'submit' ) && $this->form_validation->valid_token() )
            {
                $postId = intval( $this->input->post('postid') );
                $content = strip_tags( $this->input->post('content') );
                $commentData = array(
                    'postid' => $postId,
                    'userid' => $this->session->getUserSession('id'),
                    'content' => $content,
                );
                $commentID = $this->model_comment_add( $commentData );
                $messageData = array(
                    'title' => '评论成功',
                    'message' => '您的评论已经成功发表，4秒后跳转回刚才的页面',
                    'autoRedirect' => true,
                    'autoRedirectTimeOut' => 4,
                    'autoRedirectURL' => my_url( 'post', 'view', $postId, false ),
                );
                $messageBox = Modules::run( 'messageBox', $messageData );
                $data = array(
                    'title' => '评论成功',
                    'messageBox' => $messageBox,
                );
            }
            else
            {
                $messageData = array(
                    'title' => '操作失败',
                    'message' => '抱歉，评论失败请重试，4秒后跳转回刚才的页面',
                    'autoRedirect' => false,
                    'autoRedirectTimeOut' => 4,
                    'autoRedirectURL' => 'javascript:window.history.go(-1);',
                );
                $messageBox = Modules::run( 'messageBox', $messageData );
                $data = array(
                    'title' => '操作失败',
                    'messageBox' => $messageBox,
                );
            }
            
            $this->get_common_section( $data );
            $this->load->view( 'post/add', $data );
        }
    }

}